{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Week 5 高德API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 基本模块\n",
    "import pandas as pd\n",
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "key_luo =\"4b59dc5c00ca3df4f9ec8f00b63845a8\"# 可改"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 地理/逆地理编码\n",
    "* 思考：把用户的输入信息通过地理编码的形式转化为经纬度，更利于机器计算，再把这些经纬度转化为poi信息，给用户进行推荐，用到了推荐算法。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 地理编码\n",
    "* 将详细的结构化地址转换为高德经纬度坐标。且支持对地标性名胜景区、建筑物名称解析为高德经纬度坐标。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 地理编码 API \n",
    "# 请求方式 GET\n",
    "def Geocoding(address,city=None,batch=False,sig=None)->dict:\n",
    "    \"\"\"获取地理编码\"\"\"\n",
    "    url = 'https://restapi.amap.com/v3/geocode/geo?parameters'\n",
    "    params={\n",
    "        'key': key_luo,\n",
    "        'address':address,\n",
    "        'city':city,\n",
    "        'batch':batch,\n",
    "        'sig':sig,\n",
    "        'output':'json'\n",
    "    }\n",
    "    response = requests.get(url,params=params)\n",
    "    data = response.json()\n",
    "    return data\n",
    "# 地点 = Geocoding(address='')\n",
    "# 地点地理编码 = 地点['geocodes'][0]['location']\n",
    "# print(\"地点:\",地点地理编码)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 逆地理编码\n",
    "* 将经纬度转换为详细结构化的地址，且返回附近周边的POI、AOI信息。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 逆地理编码 API \n",
    "# 请求方式 GET\n",
    "def Reverse_geocoding(location,poitype=None,radius=None,extensions=\"base\",batch=False,roadlevel=None,sig=None,homeorcorp=None)->dict:\n",
    "    \"\"\"获取地理编码\"\"\"\n",
    "    url = 'https://restapi.amap.com/v3/geocode/regeo?parameters'\n",
    "    params={\n",
    "        'key': key_xu,\n",
    "        'location':location,\n",
    "        'poitype':poitype,\n",
    "        'radius':radius,\n",
    "        'extensions':extensions,\n",
    "        'batch':batch,\n",
    "        'roadlevel':roadlevel,\n",
    "        'homeorcorp':homeorcorp,\n",
    "        'sig':sig,\n",
    "        'output':'json'\n",
    "    }\n",
    "    response = requests.get(url,params=params)\n",
    "    data = response.json()\n",
    "    return data\n",
    "# 地点逆地理编码_base = Reverse_geocoding(地点地理编码)\n",
    "# print(地点逆地理编码_base)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 路径规划"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 步行路径规划\n",
    "* 路径规划API是一套以HTTP形式提供的步行、公交、驾车查询及行驶距离计算接口，返回JSON 或 XML格式的查询数据，用于实现路径规划功能的开发。 \n",
    "\n",
    "* 思考：该api应该可以收集人们步行到同一个目的地的时间长短，给用户推荐相应的距离短的路径。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 步行路径规划\n",
    "# 请求方式 Get\n",
    "def walking_path(origin,destination,sig=None)->dict:\n",
    "    url = 'https://restapi.amap.com/v3/direction/walking?parameters'\n",
    "    params={\n",
    "        'key': key_luo,\n",
    "        'origin':origin,\n",
    "        'destination':destination,\n",
    "        'sig':sig,\n",
    "        'output':'json'\n",
    "    }\n",
    "    response = requests.get(url,params=params)\n",
    "    data = response.json()\n",
    "    return data\n",
    "# 出发点=walking_path(origin='')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 行政区域查询\n",
    "* 根据用户输入的搜索条件可以帮助用户快速的查找特定的行政区域信息。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 行政区域查询\n",
    "# 请求方式 Get\n",
    "def administrative_division(keywords,subdistrict=None,page=None,offset=None,extensions='base',filter=None,)->dict:\n",
    "    url = 'https://restapi.amap.com/v3/config/district?parameters'\n",
    "    params={\n",
    "        'key':key_luo,\n",
    "        'keywords':keywords,\n",
    "        'subdistrict':subdistrict,\n",
    "        'page':page,\n",
    "        'offset':offset,\n",
    "        'extensions':extensions,\n",
    "        'filter':filter,\n",
    "        'output':'json'\n",
    "    }\n",
    "    response = requests.get(url,params=params)\n",
    "    data = response.json()\n",
    "    return data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 搜索POI\n",
    "* 搜索服务API是一类简单的HTTP接口，提供多种查询POI信息的能力，其中包括关键字搜索、周边搜索、多边形搜索、ID查询四种筛选机制。\n",
    "* 思考：该功能可用于推荐算法。通过获取用户的搜索内容，判断用户的喜好，进行算法推荐。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 关键字搜索\n",
    "* 通过用POI的关键字进行条件搜索，例如：肯德基、朝阳公园等；同时支持设置POI类型搜索，例如：银行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 关键字搜索\n",
    "# 请求方式 Get\n",
    "def keywords(keywords,type=None,city=None,citylimit=False,children=None,offset=None,page=None,extensions=\"base\",sig=None)->dict:\n",
    "    url = 'https://restapi.amap.com/v3/place/text?parameters'\n",
    "    params={        \n",
    "        'key':key_luo,\n",
    "        'keywords':keywords,\n",
    "        'type':type,\n",
    "        'city':city,\n",
    "        'children':children,\n",
    "        'page':page,\n",
    "        'extensions':extensions,\n",
    "        'sig':sig,\n",
    "        'output':'json'\n",
    "    }\n",
    "    response = requests.get(url,params=params)\n",
    "    data = response.json()\n",
    "    return data\n",
    "# keywords(keywords='美食',city='汕头')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## IP定位\n",
    "* 根据用户输入的IP地址，能够快速的帮用户定位IP的所在位置。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# IP定位\n",
    "# 请求方式 Get\n",
    "def ip(ip,sig)->dict:\n",
    "    url = 'https://restapi.amap.com/v3/ip?parameters'\n",
    "    params={        \n",
    "        'key':key_luo,\n",
    "        'ip':ip,\n",
    "        'sig':sig,\n",
    "        'output':'json'\n",
    "    }\n",
    "    response = requests.get(url,params=params)\n",
    "    data = response.json()\n",
    "    return data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 批量请求接口\n",
    "* 批量接口通过用户传入合并后的请求，同时返回多个请求的顺序集合，目前最多支持20个子请求。 返回的顺序与子请求的顺序一致。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "def api():\n",
    "    url = 'https://restapi.amap.com/v3/batch?key=4b59dc5c00ca3df4f9ec8f00b63845a8'\n",
    "    headers = {\n",
    "        \"content-type\":\"application/json\"\n",
    "    }  \n",
    "    body = {\n",
    "      \"ops\": [\n",
    "        {\n",
    "            \"url\": \"/v3/place/around?offset=10&page=1&key=4b59dc5c00ca3df4f9ec8f00b63845a8f&location=113.292429,23.200438&output=json&radius=100000&types=080000\"\n",
    "        },\n",
    "        {\n",
    "            \"url\": \"/v3/place/around?offset=10&page=1&key=4b59dc5c00ca3df4f9ec8f00b63845a8f&location=113.679287,23.632575&output=json&radius=100000&types=080000\"\n",
    "        }\n",
    "        ]\n",
    "      }\n",
    "    response = requests.post(url,data = json.dumps(body),headers=headers)\n",
    "    data = response.json()\n",
    "    return data "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 静态地图\n",
    "* 通过返回一张地图图片响应HTTP请求，使用户能够将高德地图以图片形式嵌入自己的网页中。\n",
    "* 思考：通过计算机视觉的图像处理，把实景图转换为静态地图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Image模块是在Python PIL图像处理中常见的模块\n",
    "# open（filename,mode）:打开一张图像\n",
    "from PIL import Image\n",
    "from io import BytesIO"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 静态地图\n",
    "# 请求方式 Get\n",
    "\n",
    "def map(location,zoom,size=None,scale=1,markers=None,labels=None,paths=None,traffic=0,sig=None)->dict:\n",
    "    url = 'https://restapi.amap.com/v3/staticmap?parameters'\n",
    "    params={\n",
    "        'key':key_luo,\n",
    "        'location':location,\n",
    "        'zoom':zoom,\n",
    "        'size':size,\n",
    "        'scale':scale,\n",
    "        'markers':markers,\n",
    "        'labels':labels,\n",
    "        'paths':paths,\n",
    "        'traffic':traffic,\n",
    "        'sig':sig,\n",
    "        'output':'json'}\n",
    "    response = requests.get(url,params=params)\n",
    "    data = Image.open(BytesIO(response.content))\n",
    "    return data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 坐标转换\n",
    "* 坐标转换是一类简单的HTTP接口，能够将用户输入的非高德坐标（GPS坐标、mapbar坐标、baidu坐标）转换成高德坐标。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 坐标转换\n",
    "# 请求方式 Get\n",
    "def coordinate(locations,coordsys=None,sig=None)->dict:\n",
    "    url = 'https://restapi.amap.com/v3/assistant/coordinate/convert?parameters'\n",
    "    params={        \n",
    "        'key':key_luo,\n",
    "        'locations':locations,\n",
    "        'sig':sig,\n",
    "        'coordsys':coordsys,\n",
    "        'output':'json'\n",
    "    }\n",
    "    response = requests.get(url,params=params)\n",
    "    data = response.json()\n",
    "    return data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 天气查询\n",
    "* 根据用户输入的adcode，查询目标区域当前/未来的天气情况。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 天气查询\n",
    "# 请求方式 Get\n",
    "def weather(city,extensions='base')->dict:\n",
    "    url = 'https://restapi.amap.com/v3/weather/weatherInfo?parameters'\n",
    "    params={        \n",
    "        'key':key_luo,\n",
    "        'city':city,\n",
    "        'extensions':extensions,\n",
    "        'output':'json'\n",
    "    }\n",
    "    response = requests.get(url,params=params)\n",
    "    data = response.json()\n",
    "    return data\n",
    "# weather(city='110000')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 输入提示\n",
    "* 提供根据用户输入的关键词查询返回建议列表。\n",
    "* 思考：该功能运用到了推荐算法，能根据用户的gps定位到用户所在地点，当用户输入的关键字等给予建议。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 输入提示\n",
    "# 请求方式 Get\n",
    "def assistant(keywords,type=None,location=None,city=None,datatype='all',sig=None)->dict:\n",
    "    url = 'https://restapi.amap.com/v3/assistant/inputtips?parameters'\n",
    "    params={        \n",
    "        'key':key_luo,\n",
    "        'keywords':keywords,\n",
    "        'type':type,\n",
    "        'location':location,\n",
    "        'city':city,\n",
    "        'datatype':datatype,\n",
    "        'sig':sig,\n",
    "        'output':'json'\n",
    "    }\n",
    "    response = requests.get(url,params=params)\n",
    "    data = response.json()\n",
    "    return data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 交通态势\n",
    "* 提供根据用户输入的内容能够返回希望查询的交通态势情况。\n",
    "* 思考：机器通过获取当前用户的行车速度，来判断路况。是否为交通堵塞或是行驶顺畅。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 交通态势\n",
    "# 请求方式 Get\n",
    "def traffic(rectangle,level=None,sig=None)->dict:\n",
    "    url = 'https://restapi.amap.com/v3/traffic/status/rectangle?parameters'\n",
    "    params={        \n",
    "        'key':key_luo,\n",
    "        'rectangle':rectangle,\n",
    "        'level':level,\n",
    "        'sig':sig,\n",
    "        'output':'json'\n",
    "    }\n",
    "    response = requests.get(url,params=params)\n",
    "    data = response.json()\n",
    "    return data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 地理围栏\n",
    "* 地理围栏服务是一类HTTP接口，提供在服务端，增删改查地理围栏的功能，同时支持对于设备与围栏关系进行监控。\n",
    "* 思考：该功能用到了推荐算法，能通过GPS定位，获取用户所在地。在线下门店促销时，当通过该功能获取到用户定位在促销门店区域的附近时，可以向用户发送引导进入店铺的促销信息。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 创建围栏"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建围栏\n",
    "# 请求方式 POST\n",
    "def geofence()->dict:\n",
    "    url='https://restapi.amap.com/v4/geofence/meta?key='+key_luo\n",
    "    headers={\n",
    "         \"content-type\":\"application/json\"\n",
    "    }\n",
    "    body={\n",
    "\"name\": \"测试围栏名称\",\n",
    "\"center\": \"115.672126,38.817129\",\n",
    "\"radius\": \"1000\",\n",
    "\"enable\": \"true\",\n",
    "\"valid_time\": \"2017-05-19\",\n",
    "\"repeat\": \"Mon,Tues,Wed,Thur,Fri,Sat,Sun\",\n",
    "\"time\": \"00:00,11:59;13:00,20:59\",\n",
    "\"desc\": \"测试围栏描述\",\n",
    "\"alert_condition\": \"enter;leave\"\n",
    "}\n",
    "    response=requests.post(url,data = json.dumps(body), headers = headers)\n",
    "    data = response.json()\n",
    "    return data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 轨迹纠偏\n",
    "* 根据坐标点抓取道路，即根据给定的坐标点、车辆的方位角以及行驶速度，将用户的轨迹纠偏到路上，从而返回用户实际驾车经过的道路坐标。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建围栏\n",
    "# 请求方式 POST\n",
    "def grasproad(key_luo):\n",
    "    url ='https://restapi.amap.com/v4/grasproad/driving '\n",
    "    headers={\n",
    "         \"content-type\":\"application/json\"\n",
    "    }\n",
    "    body=[\n",
    "        {\n",
    "        \"x\": 116.449429,\n",
    "        \"y\": 40.014844,\n",
    "        \"sp\": 4,\n",
    "        \"ag\": 110,\n",
    "        \"tm\": 1478831753\n",
    "        }, \n",
    "        {\n",
    "        \"x\": 116.449639,\n",
    "        \"y\": 40.014776,\n",
    "        \"sp\": 3,\n",
    "        \"ag\": 110,\n",
    "        \"tm\": 23\n",
    "        },\n",
    "        {\n",
    "        \"x\": 116.449859,\n",
    "        \"y\": 40.014716,\n",
    "        \"sp\": 3,\n",
    "        \"ag\": 111,\n",
    "        \"tm\": 33\n",
    "        },\n",
    "        {\n",
    "        \"x\": 116.450074,\n",
    "        \"y\": 40.014658,\n",
    "        \"sp\": 3,\n",
    "        \"ag\": 110,\n",
    "        \"tm\": 31\n",
    "        }, \n",
    "        {\n",
    "        \"x\": 116.450273,\n",
    "        \"y\": 40.014598,\n",
    "        \"sp\": 3,\n",
    "        \"ag\": 111,\n",
    "        \"tm\": 20\n",
    "        }\n",
    "    ]\n",
    "    params={        \n",
    "        'key':key_luo\n",
    "    }\n",
    "    \n",
    "    response= requests.post(url,data=json.dumps(body),headers=headers,params=params)\n",
    "    data = response.json()\n",
    "    return data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 应用设计"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 车到了\n",
    "* 车到了是一款让乘客能掌握公交出行信息的APP。它能用于查看公车到站情况的，让乘客能快速的知道自己需要的公车现在哪个站，离自己多远，还有几个站车还回到；当遇上公路重修，公交改道时，乘客也能通过该APP了解到公车是否应道路重修取消了哪些站点又增加了哪些站点；当在陌生的地方时，车到了也能根据乘客的定位，提供离乘客做最近的需要搭乘的公交车的公交车站。\n",
    "* 用到的高德API有:\n",
    "  * ip定位\n",
    "  * 路径规划中的步行路径规划、公交路径规划和距离测量\n",
    "  * 输入提示\n",
    "  * 交通态势"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "582.5px",
    "left": "329px",
    "top": "109.74px",
    "width": "315.833px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
